import { unref } from 'vue';
import { ROW_KEY } from '../const';
import { isString, isFunction } from '/@/utils/is';
function getKey(record, rowKey, autoCreateKey) {
    if (!rowKey || autoCreateKey) {
        return record[ROW_KEY];
    }
    if (isString(rowKey)) {
        return record[rowKey];
    }
    if (isFunction(rowKey)) {
        return record[rowKey(record)];
    }
    return null;
}
export function useCustomRow(propsRef, { setSelectedRowKeys, getSelectRowKeys, getAutoCreateKey, clearSelectedRowKeys, emit }) {
    const customRow = (record, index) => {
        return {
            onClick: (e) => {
                e?.stopPropagation();
                function handleClick() {
                    const { rowSelection, rowKey, clickToRowSelect } = unref(propsRef);
                    if (!rowSelection || !clickToRowSelect)
                        return;
                    const keys = getSelectRowKeys();
                    const key = getKey(record, rowKey, unref(getAutoCreateKey));
                    if (!key)
                        return;
                    const isCheckbox = rowSelection.type === 'checkbox';
                    if (isCheckbox) {
                        // 找到tr
                        const tr = e.composedPath?.().find((dom) => dom.tagName === 'TR');
                        if (!tr)
                            return;
                        // 找到Checkbox，检查是否为disabled
                        const checkBox = tr.querySelector('input[type=checkbox]');
                        if (!checkBox || checkBox.hasAttribute('disabled'))
                            return;
                        if (!keys.includes(key)) {
                            setSelectedRowKeys([...keys, key]);
                            return;
                        }
                        const keyIndex = keys.findIndex((item) => item === key);
                        keys.splice(keyIndex, 1);
                        setSelectedRowKeys(keys);
                        return;
                    }
                    const isRadio = rowSelection.type === 'radio';
                    if (isRadio) {
                        if (!keys.includes(key)) {
                            if (keys.length) {
                                clearSelectedRowKeys();
                            }
                            setSelectedRowKeys([key]);
                            return;
                        }
                        clearSelectedRowKeys();
                    }
                }
                handleClick();
                emit('row-click', record, index, e);
            },
            onDblclick: (event) => {
                emit('row-dbClick', record, index, event);
            },
            onContextmenu: (event) => {
                emit('row-contextmenu', record, index, event);
            },
            onMouseenter: (event) => {
                emit('row-mouseenter', record, index, event);
            },
            onMouseleave: (event) => {
                emit('row-mouseleave', record, index, event);
            },
        };
    };
    return {
        customRow,
    };
}
